package util; 
public class FCodigo {
    
	///////////////////Incrementar codigo informado/////////////////
	public static String icrementarcodigo(String cod, int tam, String Esp){		
		int codigo = 0;
		try {
          codigo = Integer.parseInt(cod);
          codigo = codigo + 1;          
          cod = CompletarCampo.campcodigo(String.valueOf(codigo), tam, Esp);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			cod = CompletarCampo.campcodigo("1", tam, Esp);
		}
		return cod;
	}

	///////////////////Incrementar codigo informado/////////////////
	public static String DigiAuto(String cod){
		String dig = "";
		int Dig   = 0;
		cod = CompletarCampo.campcodigo(cod, 21, " ");
		if(!cod.substring(1, 21).equals("999999999999999999999")){
			
		   int x     = 20;          
//		   int k     = 21;		   
		   int Check = 2;
		   while (x > 0) {
			      Dig   = Dig + Integer.parseInt(cod.substring(x,x+1)) * Check;
			      Check = Check + 1;
			      x     = x - 1;			
		   }
		   
		   Dig = 11 - (Dig - ((Dig / 11) * 11));
		   if(String.valueOf(Dig).length() > 2){
			   Dig = Integer.parseInt(String.valueOf(Dig).substring(0, 2));   
		   }
			
		}
		dig = String.valueOf(Dig);//CompletarCampo.campcodigo(String.valueOf(Dig), 2, "0");
		return dig;
	}	

	////////trocar letra////////
	public static String trocaletra(String strold, String letraold, String letranew){
		StringBuffer aux = new StringBuffer("");
		String ret = "";
		if(strold != null){
			for (int i = 0; i < (strold.length()); i++) {
				if(strold.substring(i, i+1).equals(letraold)){
					aux.append(letranew);
				}else{
					aux.append(strold.substring(i, i+1));	
				}
				    
			}
			ret = aux.toString();
		}
		return ret;
	}
}
